package org.basis.algorithm.tree;

import org.basis.algorithm.tree.common.Node;

import java.util.LinkedList;

/**
 * 宽度优先遍历树
 *
 * @author Mr_wenpan@163.com 2021/12/22 11:03
 */
public class WidthFirstTraversal {

    public static void main(String[] args) {

    }

    public static void widthFirstTraversal(Node head) {
        if (head == null) {
            return;
        }
        // 宽度优先用队列
        LinkedList<Node> queue = new LinkedList<>();
        queue.add(head);
        while (!queue.isEmpty()) {
            Node poll = queue.poll();
            System.out.print(poll.data + "   ");
            if (poll.left != null) {
                queue.add(poll.left);
            }
            if (poll.right != null) {
                queue.add(poll.right);
            }
        }
    }

}
